package org.example.config;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;

public class MyTableNameHandler implements TableNameHandler {

    private static final ThreadLocal<String> TABLE_NAME_SUFFIX = new ThreadLocal<>();

    public static void setTableNameSuffix(String suffix) {
        TABLE_NAME_SUFFIX.set(suffix);
    }

    @Override
    public String dynamicTableName(String sql, String tableName) {
        String suffix = TABLE_NAME_SUFFIX.get();
        if (StringUtils.isNotBlank(suffix)) {
            TABLE_NAME_SUFFIX.remove();
            String dynamicTableName = tableName + "_" + suffix;
//            log.info("dynamicTableName = {}", dynamicTableName);
            return dynamicTableName;
        }
        return tableName;
    }
}
